# -*- coding: utf-8 -*-
# Time    : 2019/11/23 14:52
# Author  : LiaoKong
try:
    from PySide2.QtWidgets import *
    from PySide2.QtGui import *
    from PySide2.QtCore import *
except:
    from PySide.QtGui import *
    from PySide.QtCore import *

import nuke

current_tool = None
excludes_list = ['xpos', 'ypos', 'name', 'selected', 'hidePanel', 'showPanel']


class BatchChangeKnobTool(QDialog):
    def __init__(self, parent=None):
        super(BatchChangeKnobTool, self).__init__(parent)

        self.parent = parent

        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

        main_layout = QVBoxLayout(self)
        label = QLabel(u'<font color=red size=6><b>批量编辑节点参数模式开启</b></font>')
        main_layout.addWidget(label)
        self.setStyleSheet('QLabel{border:1px solid red;border-radius: 6px;padding:10px}')

        self._reg_shortcut()
        self._init_ani()
        self._fade_int()

        nuke.addKnobChanged(self._change_knob)

    def _reg_shortcut(self):
        self.sel_all_action = QAction('sel_all', self)
        self.sel_all_action.setShortcut(QKeySequence("Alt+A"))
        self.parent.addAction(self.sel_all_action)
        self.sel_all_action.triggered.connect(self.sel_all_action_triggered)

    @staticmethod
    def sel_all_action_triggered():
        """选中所有同类型的节点"""
        sel_nodes = nuke.selectedNodes()
        if sel_nodes:
            for node in nuke.allNodes():
                if node.Class() == sel_nodes[0].Class():
                    node['selected'].setValue(True)
                    node.showControlPanel()

    @staticmethod
    def _change_knob():
        """当knob有修改触发"""
        knob = nuke.thisKnob()

        if knob.name() not in excludes_list:
            for node in nuke.selectedNodes():
                if node.Class() == nuke.thisNode().Class():
                    if not node[knob.name()].isAnimated():
                        node[knob.name()].setValue(knob.value())
                        if knob.Class() == 'LookupCurves_Knob':
                            node[knob.name()].fromScript(knob.toScript())

    def _init_ani(self):
        # 位置动画初始化
        self._pos_ani = QPropertyAnimation(self)
        self._pos_ani.setTargetObject(self)
        self._pos_ani.setEasingCurve(QEasingCurve.OutCubic)
        self._pos_ani.setDuration(300)
        self._pos_ani.setPropertyName('pos')

        self._set_label_position()

        # 透明度动画初始化
        self._opacity_ani = QPropertyAnimation(self)
        self._opacity_ani.setTargetObject(self)
        self._opacity_ani.setDuration(300)
        self._opacity_ani.setEasingCurve(QEasingCurve.OutCubic)
        self._opacity_ani.setPropertyName('windowOpacity')

        self._opacity_ani.setStartValue(0.0)
        self._opacity_ani.setEndValue(1.0)

    def _set_label_position(self):
        """设置提示label的动画位置"""
        parent_geo = self.parent.geometry()

        target_x = parent_geo.x() + parent_geo.width() / 2 - 100
        target_y = parent_geo.y() + 30
        self._pos_ani.setStartValue(QPoint(target_x, target_y - 50))
        self._pos_ani.setEndValue(QPoint(target_x, target_y))

    def _fade_int(self):
        """开始淡入动画"""
        self._pos_ani.start()
        self._opacity_ani.start()

    def start_close(self):
        nuke.removeKnobChanged(self._change_knob)
        self.parent.removeAction(self.sel_all_action)

        self._pos_ani.setDirection(QAbstractAnimation.Backward)
        self._pos_ani.finished.connect(self.close)

        self._opacity_ani.setDirection(QAbstractAnimation.Backward)

        self._pos_ani.start()
        self._opacity_ani.start()


def start():
    global current_tool
    parent = QApplication.activeWindow()

    if not current_tool:
        current_tool = BatchChangeKnobTool(parent)
        current_tool.show()
    else:
        current_tool.start_close()
        current_tool = None
